<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/lib/component/toolbox')
require('echarts/lib/component/legend')
require('echarts/theme/macarons') // echarts theme
import resize from '../../dashboard/mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el)

      this.chart.setOption({
        title: {
          text: '积分发放和消耗数量',
          x: 'center',
          y: 'top'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 40,
          left: '2%',
          right: '2%',
          bottom: 30,
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: ['2月', '3月', '4月', '5月', '6月', '7月'],
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [
          {
            type: 'value',
            name: '数量',
            show: true
          },
          {
            type: 'value',
            name: '概率',
            axisLabel: {
              formatter: '{value} %'
            }
          }
        ],
        legend: {
          data: ['发放', '消耗', '消耗 : 发放'],
          x: 'center',
          y: 'bottom'
        },
        series: [
          {
            name: '发放',
            type: 'bar',
            barWidth: '30%',
            data: [2000, 1500, 2500, 1000, 1400, 1100],
            animationDuration,
            itemStyle: {
              normal: {
                color: '#0c259c'
              }
            }
          },
          {
            name: '消耗',
            type: 'bar',
            barWidth: '30%',
            data: [1000, 1000, 2000, 1500, 1600, 1000],
            animationDuration,
            itemStyle: {
              normal: {
                color: '#2E8B57'
              }
            }
          },
          {
            name: '消耗 : 发放',
            type: 'line',
            yAxisIndex: 1,
            barWidth: '30%',
            data: [1000 / 20, 1000 / 15, 2000 / 25, 1500 / 10, 1600 / 14, 1000 / 11].map(value => {
              return value.toFixed(2)
            }),
            animationDuration,
            symbolSize: 10,
            itemStyle: {
              normal: {
                color: '#cd5c5c',
                lineStyle: {
                  width: 3
                }
              }
            }
          }
        ]
      })
    }
  }
}
</script>
